package de.metalcon.gwt.client.modules;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

import de.metalcon.gwt.client.mainDesign.Box;
import de.metalcon.gwt.client.pages.StreamPage;
import de.metalcon.gwt.client.pages.UserPage;
import de.metalcon.gwt.client.pages.WikiPage;
import de.metalcon.gwt.shared.ContentTransmitter;

public class WikiModule extends ADynamicModule {
	public WikiModule(String key) {
		addReuqestVar("key", key);
	}

	@Override
	protected void generateContent() {
		ContentTransmitter transmitter = getContentTransmitter();
		
		String s = "<h1>social news stream demo: on neo4j + gwt with wikipedia graph</h1>";
		s = s + "<h2>find the sourcode and join or open source project in our <a href='http://wiki.metalcon.de' target=_blank>wiki</a></h2>";
		HTML html1 = new HTML(s);
		add(html1);
		
		Box box = new Box();
		box.setWidth("300px");
		Label caption = new Label(transmitter.getString("title"));
/*		String s = "<h1>social news stream demo: on neo4j + gwt with wikipedia graph</h1>";
		s = s + "<h2>find the sourcode and join or open source project in our <a href='http://wiki.metalcon.de'>wiki</a></h2>";
		
		HTML html1 = new HTML(s);*/
		//box.add(html1);
		box.add(caption);
		
		
		/*
		for (Map.Entry<String, Object> entry : transmitter.getAllPairs()) {
			Label testContent = new Label(entry.getKey() + " : " + entry.getValue());
			box.add(testContent);
		}*/
		
		ArrayList<ContentTransmitter> tmp = null;

		caption = new Label(transmitter.getString("title") + "'s news stream with feeds from the followed articles!");
		box.add(caption);
		
		
		
				
		FlowPanel content = new FlowPanel();
		//tmp = transmitter.getList("stream");
		int max=Integer.parseInt(transmitter.getString("numnews"));
		
	/*	for (int i=0; i< max;i++){
			ContentTransmitter ct = tmp.get(i);
			for (Map.Entry<String, ArrayList<ContentTransmitter>> entry  :ct.getAllLists()){
				for (ContentTransmitter t : entry.getValue()) {
					caption = new Label(t.getString("timestamp"));
					content.add(caption);
					
					Hyperlink link = new Hyperlink(t.getString("title"), WikiPage.generateHistoryToken(new String[]{t.getString("key")}));
					content.add(link);
					caption = new Label(t.getString("."));
					content.add(caption);
				}
			}*/
		HTML html = new HTML(transmitter.getString("stream"));
		box.add(html);
/*			caption = new Label(t.getString("timestamp"));
			content.add(caption);
			
			Hyperlink link = new Hyperlink(t.getString("title"), WikiPage.generateHistoryToken(new String[]{t.getString("key")}));
			content.add(link);
			caption = new Label(t.getString("."));
			content.add(caption);*/
	//	}
		//Set<Entry<String, ArrayList<ContentTransmitter>>> map = tmp.get(0).getAllLists();
		
/*		for (Map.Entry<String, ArrayList<ContentTransmitter>> entry  :map){
			for (ContentTransmitter t : entry.getValue()) {
				caption = new Label(t.getString("timestamp"));
				content.add(caption);
				
				Hyperlink link = new Hyperlink(t.getString("title"), WikiPage.generateHistoryToken(new String[]{t.getString("key")}));
				content.add(link);
				caption = new Label(t.getString("."));
				content.add(caption);
			}
		}*/

		box.add(content);
		add(box);
		box = new Box();
		box.setWidth("300px");

		
		content = new FlowPanel();
		content.setWidth("256px");
		caption = new Label("Follower of " + transmitter.getString("title") + " (i.d. these articles link to "+ transmitter.getString("title"));
		content.add(caption);
		
		tmp = transmitter.getList("follower");
		for (Map.Entry<String, ArrayList<ContentTransmitter>> entry  :tmp.get(0).getAllLists()){
			for (ContentTransmitter t : entry.getValue()) {
				Hyperlink link = new Hyperlink(t.getString("title"), WikiPage.generateHistoryToken(new String[]{t.getString("key")}));
				content.add(link);
			}
		}

		box.add(content);
		add(box);
		box = new Box();
		box.setWidth("300px");
		
		
		content = new FlowPanel();
		content.setWidth("256px");
		caption = new Label(transmitter.getString("title") + " follows the following articles (links to them!):");
		content.add(caption);
		
		tmp = transmitter.getList("follows");
		for (Map.Entry<String, ArrayList<ContentTransmitter>> entry  :tmp.get(0).getAllLists()){
			for (ContentTransmitter t : entry.getValue()) {
				Hyperlink link = new Hyperlink(t.getString("title"), WikiPage.generateHistoryToken(new String[]{t.getString("key")}));
				content.add(link);
			}
		}

		box.add(content);		
		add(box);
	}
}
